{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4cf3024d-eaf2-4c58-9fa5-42afdf89ae61",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2c076d8f-4b14-4939-b7fb-76a6b1f1a121",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0817100830490183\n"
     ]
    }
   ],
   "source": [
    "#构造特征\n",
    "a=np.random.randn(100)\n",
    "print(np.var(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "9f99685d-b62a-4397-b1a1-bb6aba3a55ab",
   "metadata": {
    "jupyter": {
     "is_executing": true
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0065345512435322165\n"
     ]
    }
   ],
   "source": [
    "b = np.random.randn(100) * 0.1\n",
    "b= np.random.normal(5,0.1,size=100)\n",
    "print(np.var(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "45afb5e259784a2c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.50661834  4.99563721]\n",
      " [-0.49868386  4.84525588]\n",
      " [ 0.92866332  5.00492214]\n",
      " [-0.47681658  5.04105062]\n",
      " [ 1.39883021  5.01136863]\n",
      " [-0.0825277   4.97838756]\n",
      " [ 0.79665476  4.9323143 ]\n",
      " [ 0.11148056  4.90816103]\n",
      " [-0.55696883  5.01369109]\n",
      " [ 0.52122099  5.13658682]\n",
      " [-1.28555639  5.14232384]\n",
      " [ 0.87122994  4.99316943]\n",
      " [ 0.15219133  5.02012897]\n",
      " [-0.03168433  4.96916494]\n",
      " [-0.59778879  5.03162343]\n",
      " [ 1.20869253  5.01916904]\n",
      " [ 0.47403509  5.14696608]\n",
      " [-1.2414605   4.99451017]\n",
      " [ 0.2591055   5.01759119]\n",
      " [ 0.51615679  5.04308238]\n",
      " [-0.55802189  4.99666235]\n",
      " [ 1.93848041  5.00656684]\n",
      " [-0.08975272  5.01369298]\n",
      " [ 0.61496672  5.00870096]\n",
      " [ 0.09852363  4.98861739]\n",
      " [-0.65685723  5.05633188]\n",
      " [-0.70015103  5.10987101]\n",
      " [ 3.01996237  4.86088055]\n",
      " [ 0.84301125  4.9231418 ]\n",
      " [ 0.94872662  5.04572965]\n",
      " [ 0.00670661  5.13806762]\n",
      " [ 2.34541595  5.0794457 ]\n",
      " [-0.55846313  4.91813141]\n",
      " [-0.41892895  4.94378321]\n",
      " [ 1.57127397  4.95772014]\n",
      " [ 1.4778502   5.00008331]\n",
      " [-0.24211309  4.95506868]\n",
      " [ 1.91223256  4.96622533]\n",
      " [ 0.71855416  5.04558741]\n",
      " [-0.22513271  4.91030248]\n",
      " [-0.42192197  4.92869365]\n",
      " [-0.34059398  4.87502316]\n",
      " [ 0.90794602  4.93964608]\n",
      " [-1.21820534  5.0563116 ]\n",
      " [-2.57085338  5.13734258]\n",
      " [ 1.35177867  4.98472041]\n",
      " [-0.41163098  4.91054615]\n",
      " [-1.65360408  5.1754838 ]\n",
      " [-0.50308016  4.92619355]\n",
      " [-0.50150029  4.99794917]\n",
      " [ 0.07665496  5.00005264]\n",
      " [ 0.16129629  4.9710692 ]\n",
      " [ 0.26751758  4.98148681]\n",
      " [-0.23854057  4.88798843]\n",
      " [-1.11852669  4.81343059]\n",
      " [ 0.70870146  5.1079079 ]\n",
      " [ 0.10199771  5.01901295]\n",
      " [-0.79289002  5.07542471]\n",
      " [ 0.94380983  4.93046613]\n",
      " [ 1.80776445  4.99359541]\n",
      " [ 1.02497113  5.06454529]\n",
      " [ 2.49974765  5.07638915]\n",
      " [ 0.76325355  5.00079714]\n",
      " [ 0.51950757  5.01583784]\n",
      " [ 0.56467906  4.9643947 ]\n",
      " [-0.75441723  4.95811388]\n",
      " [-0.76290116  5.12341037]\n",
      " [ 0.53353845  4.92510437]\n",
      " [ 0.6938705   4.97025243]\n",
      " [ 1.13584864  4.90825251]\n",
      " [ 2.14647387  4.94203397]\n",
      " [-0.04177576  4.85261722]\n",
      " [ 2.13546398  5.07468233]\n",
      " [-1.25313722  5.04816705]\n",
      " [-0.78488644  4.95603502]\n",
      " [ 0.48220916  4.99422525]\n",
      " [-0.23506293  5.02822041]\n",
      " [ 0.76594434  5.17109082]\n",
      " [ 0.23688667  5.07447967]\n",
      " [ 1.35776725  5.14981698]\n",
      " [-1.21241048  4.90371622]\n",
      " [-0.14086605  5.00758612]\n",
      " [ 1.38981722  4.9357935 ]\n",
      " [ 0.27085253  4.99977873]\n",
      " [-0.79211193  5.11652447]\n",
      " [-0.06385781  4.94854286]\n",
      " [ 0.74750692  5.01451382]\n",
      " [ 0.83927101  5.07958635]\n",
      " [-0.29797645  4.91274132]\n",
      " [ 1.96613782  4.95585789]\n",
      " [-2.3326      5.02265874]\n",
      " [-0.16694085  4.97395618]\n",
      " [ 0.59416517  5.04424802]\n",
      " [-0.70458743  4.86559465]\n",
      " [ 0.70049903  4.93179402]\n",
      " [ 0.6326416   4.97355094]\n",
      " [ 1.24046199  4.92814001]\n",
      " [-1.00353042  5.15379613]\n",
      " [-2.19972476  4.76417235]\n",
      " [ 0.15847191  5.02573551]]\n",
      "(100, 2)\n"
     ]
    }
   ],
   "source": [
    "#构造特征向量（输入数据X）\n",
    "X = np.vstack((a,b)).T\n",
    "print(X)\n",
    "print(X.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "2b4d22cb-07c6-4fde-b952-6e4a2a116913",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.50661834]\n",
      " [-0.49868386]\n",
      " [ 0.92866332]\n",
      " [-0.47681658]\n",
      " [ 1.39883021]\n",
      " [-0.0825277 ]\n",
      " [ 0.79665476]\n",
      " [ 0.11148056]\n",
      " [-0.55696883]\n",
      " [ 0.52122099]\n",
      " [-1.28555639]\n",
      " [ 0.87122994]\n",
      " [ 0.15219133]\n",
      " [-0.03168433]\n",
      " [-0.59778879]\n",
      " [ 1.20869253]\n",
      " [ 0.47403509]\n",
      " [-1.2414605 ]\n",
      " [ 0.2591055 ]\n",
      " [ 0.51615679]\n",
      " [-0.55802189]\n",
      " [ 1.93848041]\n",
      " [-0.08975272]\n",
      " [ 0.61496672]\n",
      " [ 0.09852363]\n",
      " [-0.65685723]\n",
      " [-0.70015103]\n",
      " [ 3.01996237]\n",
      " [ 0.84301125]\n",
      " [ 0.94872662]\n",
      " [ 0.00670661]\n",
      " [ 2.34541595]\n",
      " [-0.55846313]\n",
      " [-0.41892895]\n",
      " [ 1.57127397]\n",
      " [ 1.4778502 ]\n",
      " [-0.24211309]\n",
      " [ 1.91223256]\n",
      " [ 0.71855416]\n",
      " [-0.22513271]\n",
      " [-0.42192197]\n",
      " [-0.34059398]\n",
      " [ 0.90794602]\n",
      " [-1.21820534]\n",
      " [-2.57085338]\n",
      " [ 1.35177867]\n",
      " [-0.41163098]\n",
      " [-1.65360408]\n",
      " [-0.50308016]\n",
      " [-0.50150029]\n",
      " [ 0.07665496]\n",
      " [ 0.16129629]\n",
      " [ 0.26751758]\n",
      " [-0.23854057]\n",
      " [-1.11852669]\n",
      " [ 0.70870146]\n",
      " [ 0.10199771]\n",
      " [-0.79289002]\n",
      " [ 0.94380983]\n",
      " [ 1.80776445]\n",
      " [ 1.02497113]\n",
      " [ 2.49974765]\n",
      " [ 0.76325355]\n",
      " [ 0.51950757]\n",
      " [ 0.56467906]\n",
      " [-0.75441723]\n",
      " [-0.76290116]\n",
      " [ 0.53353845]\n",
      " [ 0.6938705 ]\n",
      " [ 1.13584864]\n",
      " [ 2.14647387]\n",
      " [-0.04177576]\n",
      " [ 2.13546398]\n",
      " [-1.25313722]\n",
      " [-0.78488644]\n",
      " [ 0.48220916]\n",
      " [-0.23506293]\n",
      " [ 0.76594434]\n",
      " [ 0.23688667]\n",
      " [ 1.35776725]\n",
      " [-1.21241048]\n",
      " [-0.14086605]\n",
      " [ 1.38981722]\n",
      " [ 0.27085253]\n",
      " [-0.79211193]\n",
      " [-0.06385781]\n",
      " [ 0.74750692]\n",
      " [ 0.83927101]\n",
      " [-0.29797645]\n",
      " [ 1.96613782]\n",
      " [-2.3326    ]\n",
      " [-0.16694085]\n",
      " [ 0.59416517]\n",
      " [-0.70458743]\n",
      " [ 0.70049903]\n",
      " [ 0.6326416 ]\n",
      " [ 1.24046199]\n",
      " [-1.00353042]\n",
      " [-2.19972476]\n",
      " [ 0.15847191]]\n",
      "(100, 1)\n"
     ]
    }
   ],
   "source": [
    "#低方差过滤\n",
    "from sklearn.feature_selection import VarianceThreshold\n",
    "vt = VarianceThreshold(0.01)#设置筛选阈值为0.01\n",
    "\"\"\"\n",
    "这里的0.01表示：方差小于0.01的特征会被认为是 \"低信息量特征\"，将被过滤掉\n",
    "原理：如果一个特征的方差很小（比如所有样本在这个特征上的值几乎相同），说明它对区分样本的贡献很小，保留这类特征可能会增加模型复杂度而不会提升性能\n",
    "\"\"\"\n",
    "X_filtered = vt.fit_transform(X)#对输入数据X（通常是一个二维数组，形状为 [样本数，特征数]）执行拟合和转换操作\n",
    "\"\"\"\n",
    "fit：计算X中每个特征的方差，并确定哪些特征的方差大于等于0.01（需要保留的特征）\n",
    "transform：根据筛选结果，只保留方差符合条件的特征，生成过滤后的新数据X_filtered\n",
    "\"\"\"\n",
    "print(X_filtered)\n",
    "print(X_filtered.shape)\n",
    "\"\"\"\n",
    "执行后变成了1列，是因为刚才b的方差是小于0.1的所以被过滤掉了\n",
    "因为是随机生成数据，所以如果b的方差大于0.1就可能会留下2列\n",
    "b的方差：0.0065345512435322165，所以被过滤了\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c6b2ec1d-57b5-4c0f-9945-d2d63c83b5c1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
